# -*- coding: utf-8 -*-
import re,requests,datetime,time,json
from stationinfo import chezhan_code,chezhan_names
def showTicket(html):
    html = json.loads(html)
    for i in html['data']['result']:
        name = [
                    "station_train_code",
                    "from_station_name",
                    "to_station_name",
                    "start_time",
                    "arrive_time",
                    "lishi",
                    "swz_num",
                    "zy_num",
                    "ze_num",
                    "dw_num",
                    "gr_num",
                    "rw_num",
                    "yw_num",
                    "rz_num",
                    "yz_num",
                    "wz_num",
                    "qt_num",
                    "note_num"
               ]

        data = {
                    "station_train_code": '',
                    "from_station_name": '',
                    "to_station_name": '',
                    "start_time": '',
                    "arrive_time": '',
                    "lishi": '',
                    "swz_num": '',
                    "zy_num": '',
                    "ze_num": '',
                    "dw_num": '',
                    "gr_num": '',
                    "rw_num": '',
                    "yw_num": '',
                    "rz_num": '',
                    "yz_num": '',
                    "wz_num": '',
                    "qt_num": '',
                    "note_num": ''
                }
        item = i.split('|')                                 #使用“|”进行分割
        data["station_train_code"]  = item[3]               #获取车次信息，在3号位置
        data["from_station_name"]   = item[6]               #始发站信息在6号位置
        data["to_station_name"]     = item[7]               #终点站信息在7号位置
        data["start_time"]          = item[8]               #出发时间在8号位置
        data["arrive_time"]         = item[9]               #抵达时间在9号位置
        data["lishi"]               = item[10]              #经历时间在10号位置
        data["swz_num"]             = item[32] or item[25]  #特别注意，商务座在32或25位置
        data["zy_num"]              = item[31]              #一等座信息在31号位置
        data["ze_num"]              = item[30]              #二等座信息在30号位置
        data["gr_num"]              = item[21]              #高级软卧信息在21号位置
        data["rw_num"]              = item[23]              #软卧信息在23号位置
        data["dw_num"]              = item[27]              #动卧信息在27号位置
        data["yw_num"]              = item[28]              #硬卧信息在28号位置
        data["rz_num"]              = item[24]              #软座信息在24号位置
        data["yz_num"]              = item[29]              #硬座信息在29号位置
        data["wz_num"]              = item[26]              #无座信息在26号位置
        data["qt_num"]              = item[22]              #其他信息在22号位置
        data["note_num"]            = item[1]               #备注信息在1号位置
        tickets = []
        cont = []
        cont.append(data)
        for x in cont:
            tmp = []
            for y in name:
                if y == "from_station_name":
                    tmp.append(s)
                elif y == "to_station_name":
                    tmp.append(s)
                elif y == "start_time":
                    tmp.append(s)
                elif y == "arrive_time":
                    tmp.append(s)
                elif y == "station_train_code":
                    tmp.append(s)
                else:
                    tmp.append(data[y])
            tickets.append(tmp)
        return tickets
def get(date,from_station,to_station):
    url          = "https://kyfw.12306.cn/otn/leftTicket/queryZ?"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400"
    }
    url=url+"leftTicketDTO.train_date="+date+"&leftTicketDTO.from_station="+from_station+"&leftTicketDTO.to_station="+to_station+"&purpose_codes=ADULT"
    #print(url) 已经检查过生成的URL是正确的
    #request请求获取主页
    r = requests.get(url,headers=headers)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return showTicket(r.text)